package com.dongge.dao;

import com.dongge.entity.Comment;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 * Created by Administrator on 2015/10/17.
 */
public class CommentDao {
    private SessionFactory sessionFactory;

    protected Session getSession(){
        return  sessionFactory.getCurrentSession();
    }

    /**
     * 增
     * @param model
     */
    public void save(Comment model){
        if(model!=null)
        {
            getSession().save(model);
        }
    }

    /**
     * 改
     * @param model
     */
    public void update(Comment model){
        if(model!=null){
            getSession().update(model);
        }
    }

    /**
     * 查一个
     * @param id
     */
    public Comment get(Long id){
        if(id != null){
            return (Comment)getSession().get(Comment.class, id);
        }
        return  null;
    }

    /**
     * 查询所有
     * @return
     */
    public List<Comment> getAll(){
        String hsql = "from Comment";
        return getSession().createQuery(hsql).list();
    }

    /**
     * 删
     * @param id
     */
    public void delete(Long id){
        Comment model = get(id);
        if(model != null)
        {
            getSession().delete(model);
        }
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
}
